<template>
  <div>
    <el-row>
      <el-col :span="4">
        <el-input
          placeholder="分类名称查询"
          v-model="page.className"
          maxlength="20"
        >
        </el-input>
      </el-col>
      <el-col :span="2">
        <el-button icon="el-icon-search" @click="search" type="success"
          >搜索
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button icon="el-icon-delete" @click="reset" type="info"
          >重置
        </el-button>
      </el-col>
      <el-col :span="2" :offset="14"
        ><el-button type="warning" @click="handleAdd"
          >添加分类</el-button
        ></el-col
      >
    </el-row>
    <el-table :data="classList" border>
      <el-table-column prop="id" label="分类id" align="center">
      </el-table-column>
      <el-table-column prop="className" label="分类名称"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)"
            >编辑</el-button
          >&nbsp;&nbsp;&nbsp;&nbsp;
          <el-popconfirm
            title="确认删除？"
            confirm-button-text="确认"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            @confirm="handleDelete(scope.row.id)"
          >
            <el-button
              slot="reference"
              type="text"
              size="small"
              style="color: red"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[6, 21, 27, 36]"
        :page-size="page.pageSize"
        layout="total, prev, pager, next, sizes"
        :total="page.total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="分类"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-input
        v-model="classInfo.className"
        placeholder="请输入分类名称"
        maxlength="20"
      >
      </el-input>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSaveClass">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getClass, deleteClass, getOne, saveClass } from "../api/bookClassMG";
export default {
  name: "ClassView",
  data() {
    return {
      dialogVisible: false,
      page: {
        currentPage: 1,
        pageSize: 6,
        total: 20,
        className: "",
      },
      classList: [],
      classInfo: {},
    };
  },
  methods: {
    reset(){
      //点击重置
      this.page.currentPage = 1;
      this.page.className="",
      this.getClassList();
    },
    search() {
      //点击搜索
      this.page.currentPage = 1;
      this.getClassList();
    },
    async handleSaveClass() {
      if (this.classInfo.className==undefined || this.classInfo.className.length < 2) {
        this.$message({
          showClose: true,
          message: "分类名称必须位2-20个字符",
          type: "error",
        });
        return;
      }
      //保存编辑或者添加的分类信息
      await saveClass(this.classInfo).then((res) => {
        if (res.data.code == 200) {
          //保存成功之后就重新初始化数据,然后关闭dialog
          this.classInfo = {};
          this.getClassList();
          this.dialogVisible = false;
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error",
          });
        }
      });
    },
    handleClose(done) {
      //关闭弹出框，清空class对象,
      this.classInfo = {};
      done();
    },
    handleAdd(){
      //点击添加分类，显示出dialog即可
      this.dialogVisible = true
    },
    async handleEdit(id) {
      //点击编辑分类，先去查询，然后再复制到class对象里面
      // console.log(id)
      await getOne(id).then((res) => {
        if (res.data.code == 200) {
          this.classInfo = res.data.data;
          this.dialogVisible = true;
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error",
          });
        }
      });
    },
    async handleDelete(id) {
      // console.log(id)
      //删除选中的分类
      await deleteClass(id).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "success",
          });
          this.getClassList();
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error",
          });
        }
      });
    },
    async getClassList() {
      //获取所有分类的列表
      await getClass(this.page).then((res) => {
        if (res.data.code == 200) {
          this.page.total = res.data.data.total;
          this.classList = res.data.data.list;
        } else {
          this.$message({
            showClose: true,
            message: res.data.msg,
            type: "error",
          });
        }
      });
    },
    handleSizeChange(pageSize) {
      //改变每页尺寸触发事件
      this.page.pageSize = pageSize;
      //重新获取列表数据
      this.getClassList();
    },
    handleCurrentChange(currentPage) {
      //改变当前页也会触发事件
      this.page.currentPage = currentPage;
      //重新获取列表数据
      this.getClassList();
    },
  },
  mounted() {
    this.getClassList();
  },
};
</script>
  <style lang="scss" scoped>
</style>